<?php
class Info extends BZ_Controller
{
    public function _init()
    {
        $this->load->helper('common');
        $this->load->model('scenery_base_model', 'scenery_base');
        $this->load->model('goods_base_model', 'goods_base');
        $this->load->model('scenery_region_model', 'scenery_region');
        $this->load->model('region_model', 'region');
        $this->load->model('scenery_reviews_model', 'scenery_reviews');
        $this->load->model('scenery_enshrine_model', 'scenery_enshrine');
    }
    
    /**
     * 景区详情页
     * @param unknown $sid
     */
    public function index($sid)
    {
        $result = $this->scenery_base->findById($sid);
        if ($result->num_rows() <= 0) { //景区不存在
            show_404();
        }
        $sceneryBase = $result->row();
        $data['sceneryBase'] = $sceneryBase;
        $data['headTitle'] = $sceneryBase->name_hanzi;
        
        $result1 = $this->goods_base->findByUidParams(array('uid'=>$sceneryBase->uid, 'orderby'=>'cat_id ASC, goods_id ASC'));
        $goodsBase = array();
        foreach ($result1->result() as $item) {
            $goodsBase[$item->cat_id][] = $item;
        }
        $data['goodsBase'] = $goodsBase;
        $data['reviewsTotal'] = $this->scenery_reviews->total(array('sid'=>$sceneryBase->sid));
        $data['scenicNearby']  = $this->scenery_base->sceneryHotTickets(array('province_id'=>$sceneryBase->provinceid, 'not_sid'=>$sceneryBase->sid, 'limit'=>6));//看了又看
        $data['cat_id'] = array(1=>'普通票', 2=>'联票', 8=>'年票', 16=>'团队票');
        $data['cat_icon'] = array(1=>'&#xe615;', 2=>'&#xe617;', 8=>'&#xe616;', 16=>'&#xe619;');
        $this->load->view('scenic/info/index', $data);
    }
    
    //验证景区是否收藏
    public function enshrine()
    {
        if (!$this->uid) {
            echo json_encode(array(
                'status' => false,
            ));exit;
        }
        $sid = (int)$this->input->get('sid');
        $result = $this->scenery_enshrine->findByEnshrine(array('uid'=>$this->uid, 'sid'=>$sid));
        if ($result->num_rows() <= 0) { //景区不存在
            echo json_encode(array(
                'status' => false,
            ));exit;
        } else {
            echo json_encode(array(
                'status' => true,
            ));exit;
        }
    }
    
    //设置景区是否收藏
    public function setEnshrine()
    {
        if (!$this->uid) {
            echo json_encode(array(
                'status'   => false,
                'messages' => site_url('login/login/index')
            ));exit;
        }
        $sid = (int)$this->input->post('sid');
        $insert = false;
        $delete = false;
        $result = $this->scenery_enshrine->findByEnshrine(array('uid'=>$this->uid, 'sid'=>$sid));
        if ($result->num_rows() > 0) {
            $delete = $this->scenery_enshrine->deleteByEnshrine(array('uid'=>$this->uid, 'sid'=>$sid));
        } else {
            $insert = $this->scenery_enshrine->saveEnshrine(array('uid'=>$this->uid, 'sid'=>$sid));
        }
        if ($insert || $delete) {
            echo json_encode(array(
                'status'   => true,
                'messages' => $insert ? '收藏成功' : '取消收藏成功',
            ));exit;
        } else {
            echo json_encode(array(
                'status'   => false,
                'messages' => '服务器异常，请稍候再试'
            ));exit;
        }
    }
    
    //景区评论
    public function reviews($sid)
    {
        $result = $this->scenery_base->findById($sid);
        if ($result->num_rows() <= 0) { //景区不存在
            show_404();
        }
        $sceneryBase = $result->row();
        $data['sceneryBase'] = $sceneryBase;
        $data['headTitle'] = $sceneryBase->name_hanzi;
        $favourable = $this->scenery_reviews->favourableTotal(array('sid'=>$sid), true); //统计好评总数
        $data['favourable'] = $favourable;
        $totalRows = 0; //评价总数
        $goodsAble = 0; //好评总数
        foreach ($favourable as $star=>$count) {
            $totalRows += $count;
            if ($star >= 3) { //好评数
                $goodsAble += $count;
            }
        }
        $data['totalRows'] = $totalRows;
        $data['goodsAble'] = $goodsAble;
        $this->load->view('scenic/info/reviews', $data);
    }
    
    public function getReviews()
    {
        $pg = (int)$this->input->get('pg');
        $sid = (int)$this->input->get('sid');
        $pageNum = 10;
        $num = ($pg - 1) * $pageNum;
        $data['pg_list'] = $this->scenery_reviews->page_list($pageNum, $num, array('sid'=>$sid));
        $numRows = $data['pg_list']->num_rows();
        echo json_encode(array(
            'num'  => $numRows,
            'html' => $this->load->view('scenic/info/reviewsDetail', $data, true),
        ));
        exit;
    }
    
    //查看景区图片
    public function images($sid)
    {
        $result = $this->scenery_base->findById($sid);
        if ($result->num_rows() <= 0) { //景区不存在
            show_404();
        }
        $sceneryBase = $result->row();
        $data['sceneryBase'] = $sceneryBase;
        $data['headTitle'] = $sceneryBase->name_hanzi;
        $this->load->view('scenic/info/images', $data);
    }
    
    //收藏景区
    public function favorite()
    {
        $data = array();
        echo json_encode(array(
            'status'   => true,
            'messages' => '收藏成功'
        ));
    }
    
    //地图
    public function map($sid)
    {
        $result = $this->scenery_base->findById($sid);
        if ($result->num_rows() <= 0) { //景区不存在
            show_404();
        }
        $sceneryBase = $result->row();
        $data['sceneryBase'] = $sceneryBase;
        $data['headTitle'] = $sceneryBase->name_hanzi;
        $this->load->view('scenic/info/map', $data);
    }
    
    /**
     * 省份专区
     */
    public function zhuanqu($regionPinyin='zhejiang')
    {
        $region_id   = 31; //31 代表浙江
        $region_name = '浙江'; //31 代表浙江
        $result = $this->scenery_region->find(); //全国旅游导航
        foreach ($result->result() as $item) {
            if ($item->region_id == $regionPinyin || $item->region_pinyin == $regionPinyin) {
                $region_id     = $item->region_id;
                $region_name   = $item->region_name;
                break;
            }
        }
        $data['sceneryRegion'] = $result->result();
        
        $result2 = $this->region->findParentId($region_id);
        $cityids = array();
        foreach ($result2->result() as $item) {
            $cityids[$item->region_name] = $item->region_id;
        }
        $data['citys'] = $cityids;

        $result3 = $this->scenery_base->getSceneryList(array('cityids'=>$cityids));
        $rows = array();
        foreach ($result3->result() as $row) {
            $rows[$row->cityid][] = $row;
        }
        $data['sceneryListAll'] = $result3->result();
        $data['getSceneryList'] = $rows;
        $data['region_name']    = $region_name;
        $data['region_pinyin']  = $regionPinyin;
        $this->load->view('scenic/info/zhuanqu', $data);
    }
}